Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash related to useEffect returning a value instead of a function #78708

Merged
merged 1 commit into from
Jun 27, 2023

Conversation

noahtallen
Copy link
Member

@noahtallen noahtallen commented Jun 27, 2023

Related to: #77046

Proposed Changes

Resolves https://a8c.sentry.io/issues/4279026430/?project=6313676

Sentry detected a page crash in this scenario:

  1. Go to https://wordpress.com/people/add-subscribers/:site:
  2. Navigate back and forth between the two tabs
  3. After once or twice going back or forth (or even just waiting a moment), the page will crash.

After testing this locally, I found warnings in the console about useEffect returning a value. This also lead me to a Stackoverflow post: https://stackoverflow.com/questions/74265321/uncaught-typeerror-destroy-is-not-a-function-error-in-react

The issue is that when useEffect's callback returns a value, React will try to call it as a function. (This function can be used to destroy or unsubscribe from things set up in the effect.)

When that value is not a function, the page crashes.

So I went through several useEffect calls on the related pages and updated several inline effect calls to not return values. We had several which were doing like useEffect( () => ifCheck && doSomething() ). These can return booleans, which causes the crash. This is probably new behavior in React 18.

I fixed this just be wrapping these callbacks in brackets (e.g. useEffect( () => { ifCheck && doSomething() } )), which should be functionally the same.

We will also need to add a linter rule and check this doesn't happen elsewhere.

Testing Instructions

Follow the above bug-reproduction steps and check that there is no crash.

@github-actions
Copy link

github-actions bot commented Jun 27, 2023

@noahtallen noahtallen requested a review from bogiii June 27, 2023 21:01
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jun 27, 2023
@noahtallen noahtallen requested a review from a team June 27, 2023 21:01
@noahtallen noahtallen self-assigned this Jun 27, 2023
@noahtallen noahtallen changed the title Fix several instances of useEffect returning a value Fix crash related to useEffect returning a value instead of a function Jun 27, 2023
@noahtallen noahtallen merged commit 0222392 into trunk Jun 27, 2023
12 checks passed
@noahtallen noahtallen deleted the fix-use-effect-return-issues branch June 27, 2023 21:06
@noahtallen
Copy link
Member Author

I'm merging to resolve the page crash asap.

@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jun 27, 2023
@matticbot
Copy link
Contributor

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

@noahtallen
Copy link
Member Author

I did some searching around, and the types are supposed to handle this situation. However, the types don't forbid callbacks typed as () => any, which is exactly what many of these were -- this allows our weak types to slip in, causing crashes! I described this more and reported the issue in the DT repo: DefinitelyTyped/DefinitelyTyped#65890

Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good FWIW, thanks for the swift fixes 👍

@bogiii
Copy link
Contributor

bogiii commented Jun 28, 2023

Wow. Didn't know about this. @noahtallen, thanks for the quick fix!

FYI -> @Automattic/caribou

@noahtallen
Copy link
Member Author

Didn't know about this

Yeah, neither did I, to be honest! I'll p2 about it today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants